package graphicalUserInterface;

import java.io.IOException;
import java.util.Calendar;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.xml.sax.SAXException;

import settings.MasterController;
import entity.Goal;
import exception.NullVariableFound;
//@author U096819H
public class EditRemoveGoal extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;
	private ProgressBar progressBar;
	private DateTime dateTime;
	private Button btnActive;

	//@author U096819H
	public EditRemoveGoal(Shell parent, int style) {
		super(parent, style);
		setText("Edit Goal");
	}

	//@author U096819H
	public void open(Goal _g, final MasterController mc) {
		createContents(_g, mc);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	//@author U096819H
	private void createContents(final Goal _g, final MasterController mc) {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(400, 290);
		shell.setText(getText());
		shell.setLayout(new FormLayout());

		final Composite composite = new Composite(shell, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.bottom = new FormAttachment(0, 252);
		fd_composite.right = new FormAttachment(0, 384);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(3, false));

		Label lblTitle = new Label(composite, SWT.NONE);
		lblTitle.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblTitle.setText("Title");

		text = new Text(composite, SWT.BORDER);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_text.widthHint = 263;
		text.setLayoutData(gd_text);
		text.setText(_g.getName());

		Label lblTotalAmount = new Label(composite, SWT.NONE);
		lblTotalAmount.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblTotalAmount.setText("Total Amount");

		text_1 = new Text(composite, SWT.BORDER);
		text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		text_1.setText(String.valueOf(_g.getTotalAmount()));

		Label lblAmountAchieved = new Label(composite, SWT.NONE);
		lblAmountAchieved.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblAmountAchieved.setText("Amount Achieved");

		text_2 = new Text(composite, SWT.BORDER);
		text_2.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				progressBar.setSelection((int) Double.parseDouble(text_2
						.getText()));
			}
		});
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		text_2.setText(String.valueOf(_g.getAmountAchieved()));
		text_2.setEnabled(false);

		Label lblComment = new Label(composite, SWT.NONE);
		lblComment.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblComment.setText("Comment");

		text_3 = new Text(composite, SWT.BORDER);
		text_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2,
				1));
		text_3.setText(_g.getComment());

		Label lblStartDate = new Label(composite, SWT.NONE);
		lblStartDate.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblStartDate.setText("Start Date");

		final DateTime dateTime_1 = new DateTime(composite, SWT.BORDER
				| SWT.DROP_DOWN);
		new Label(composite, SWT.NONE);
		dateTime_1.setDate(_g.getStartDate().get(1), _g.getStartDate().get(2),
				_g.getStartDate().get(5));

		Label lblEndDate = new Label(composite, SWT.NONE);
		lblEndDate.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblEndDate.setText("End Date");

		dateTime = new DateTime(composite, SWT.BORDER | SWT.DROP_DOWN);
		dateTime.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		dateTime.setDate(_g.getEndDate().get(1), _g.getEndDate().get(2), _g
				.getEndDate().get(5));

		btnActive = new Button(composite, SWT.CHECK);
		btnActive.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		btnActive.setText("Active");
		btnActive.setSelection(_g.isActive());

		Label lblProgress = new Label(composite, SWT.NONE);
		lblProgress.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblProgress.setText("Progress");

		progressBar = new ProgressBar(composite, SWT.NONE);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 2, 1));
		progressBar.setMaximum((int) _g.getTotalAmount());
		progressBar.setMinimum(0);
		progressBar.setSelection((int) Double.parseDouble(text_2.getText()));
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);

		Button btnUpdate = new Button(composite, SWT.NONE);
		btnUpdate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ConfirmPrompt cnp = new ConfirmPrompt(composite.getShell(), 65);
				if (cnp.open("Do you want to update goal?")) {
					try {
						Calendar ed = Calendar.getInstance();
						ed.set(dateTime.getYear(), dateTime.getMonth(),
								dateTime.getDay());
						Calendar sd = Calendar.getInstance();
						sd.set(dateTime_1.getYear(), dateTime_1.getMonth(),
								dateTime_1.getDay());

						if (ed.after(sd)) {
							mc.UpdateGoal(_g.getId(), text.getText(), sd, ed,
									Double.parseDouble(text_1.getText()),
									_g.getAmountAchieved(), text_3.getText(),
									btnActive.getSelection(), _g.isAchieved());
							NotifyPrompt ntp = new NotifyPrompt(composite
									.getShell(), 65);
							ntp.open("Goal Updated.");
							shell.close();
						} else {
							NotifyPrompt ntp = new NotifyPrompt(composite
									.getShell(), 65);
							ntp.open("Invalid date.");
						}
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						NotifyPrompt ntp = new NotifyPrompt(composite
								.getShell(), 65);
						ntp.open("Error in updating goal.");
						e1.printStackTrace();
					}
				}
			}
		});
		GridData gd_btnUpdate = new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnUpdate.widthHint = 85;
		btnUpdate.setLayoutData(gd_btnUpdate);
		btnUpdate.setText("Update");

		Button button = new Button(composite, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				ConfirmPrompt cnp = new ConfirmPrompt(composite.getShell(), 65);
				if (cnp.open("Do you want to delete goal?")) {

					try {
						mc.RemoveGoal(_g.getId());
						NotifyPrompt ntp = new NotifyPrompt(composite
								.getShell(), 65);
						ntp.open("Goal Deleted.");
						shell.close();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						NotifyPrompt ntp = new NotifyPrompt(composite
								.getShell(), 65);
						ntp.open("Error in deleting goal.");
						e1.printStackTrace();
					}
				}
			}
		});
		GridData gd_button = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_button.widthHint = 85;
		button.setLayoutData(gd_button);
		button.setText("Delete");

	}
}
